package jman.jan23;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
	public User getUserbyName(String name) throws SQLException{
		DatebaseManager dm=new DatebaseManager();
		dm.start();
		String sql="select * from user where username='"+name+"'";
		String pwd="";
		ResultSet rs=dm.read(sql);
		while(rs.next()){
			pwd=rs.getString("password");
		}
		User u=new User(name,pwd);
		dm.close();
		return u;
	}
	public boolean addUser(User u){
		boolean flag=false;
		String sql="insert into user (username,password) values ('"+u.getName()+"','"+u.getPwd()+"')";
		DatebaseManager dm=new DatebaseManager();
		dm.start();
		if(dm.add(sql)){
			flag=true;
		}
		dm.close();
		return flag;
	}
	public static void main(String[] args){
		UserDao ud=new UserDao();
		try {
			User u=ud.getUserbyName("cheng");
			System.out.println(u.getPwd());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
